/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import BO.Controller;
import BO.User;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Wase
 */
@ManagedBean
@RequestScoped
public class LoginBean {

    private String username;
    private String password;
    private boolean loggedIn;
    private Controller  controller;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    public String validate(String username, String password) {
        User u = controller.validate(username, password);
 
        if(u != null){
            loggedIn = true;
            return "successLogin";
        }
        loggedIn = false;
        return "failLogin";
    }
    
    public String getLoggedIn(){
        if(loggedIn)
            return "true";
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("login.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "false";
    }
    
    /** Creates a new instance of LoginBean */
    public LoginBean() {
        controller = new Controller();
    }
}
